package com.chat.demo.userservice;

import com.chat.demo.userservice.DTO.Group.UserJoinedRoomDTO;
import com.chat.demo.userservice.Entity.Entity.ChatRoom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/chatroom")
public class ChatRoom_Controller {
    @Autowired
    private ChatRoom_Service service;

    @PostMapping("/create")
    public ResponseEntity<?> createRoom(@RequestBody ChatRoom room) {
        Long id = service.createRoom(room);
        return ResponseEntity.ok(Map.of("roomId", id));
    }

    @GetMapping("/user/{userId}")
    public ResponseEntity<?> getRoomsByUserId(@PathVariable int userId) {
        List<Long> roomIds = service.getRoomIdsByUserId(userId);
        return ResponseEntity.ok(Map.of("rooms", roomIds));
    }

    @DeleteMapping("/{roomId}")
    public ResponseEntity<?> deleteRoom(@PathVariable int roomId, @RequestParam int userId) {
        if (service.deleteRoom(roomId, userId)) return ResponseEntity.ok("deleted");
        return ResponseEntity.status(403).body("Not allowed");
    }

    @PostMapping("/{roomId}/invite")
    public ResponseEntity<?> invite(@PathVariable int roomId, @RequestParam int inviterId, @RequestParam int userId, @RequestParam String role) {
        if (service.inviteUser(roomId, inviterId, userId, role)) return ResponseEntity.ok("invited");
        return ResponseEntity.status(403).body("Not allowed");
    }

    @PostMapping("/{roomId}/role")
    public ResponseEntity<?> updateRole(@PathVariable int roomId, @RequestParam int adminId, @RequestParam int userId, @RequestParam String role) {
        if (service.updateUserRole(roomId, adminId, userId, role)) return ResponseEntity.ok("role updated");
        return ResponseEntity.status(403).body("Not allowed");
    }

    @DeleteMapping("/{roomId}/remove")
    public ResponseEntity<?> removeUser(@PathVariable int roomId, @RequestParam int adminId, @RequestParam int userId) {
        if (service.removeUser(roomId, adminId, userId)) return ResponseEntity.ok("user removed");
        return ResponseEntity.status(403).body("Not allowed");
    }

    @PostMapping("/{roomId}/join")
    public ResponseEntity<?> join(@PathVariable int roomId, @RequestParam int userId) {
        if (service.joinRoom(roomId, userId)) return ResponseEntity.ok("joined");
        return ResponseEntity.badRequest().body("join failed");
    }

    @PostMapping("/{roomId}/leave")
    public ResponseEntity<?> leave(@PathVariable int roomId, @RequestParam int userId) {
        if (service.leaveRoom(roomId, userId)) return ResponseEntity.ok("left");
        return ResponseEntity.badRequest().body("leave failed");
    }

    @GetMapping("/joined/{userId}")
    public ResponseEntity<?> getJoinedRooms(@PathVariable Long userId) {
        List<UserJoinedRoomDTO> rooms = service.getJoinedRoomsByUserId(userId);
        return ResponseEntity.ok(rooms);
    }
}
